# -*- coding: utf-8 -*-
#!/usr/bin/python
from plp import *
import sys
plp_init()

filesList = ["Extras","Places","Objects","Events"]
elements ={}
noteStat = {}
note = "W teatrze jest kot i opera i premiera"
level = 200




def readFile(fileName):
	output=""
	elements[fileName]={}
	file = open(fileName)
	for line in file:
		word, weight = line.split()
		for id in plp_rec(word):
			elements[fileName][id]=int(weight)
			#output+=(word+" ----> "+weight+"<p/>")
	return output
			
def scoreInit():
	for e in  elements.keys():
		noteStat[e]=0
	noteStat["total"]=0
	
def checkWord(word):
	output=""
	for e in  elements.keys():
		for id1 in elements[e].keys():
			for id2 in plp_rec(word):
				if (id1 == id2):
					output+=("Word \"" + word+"\" is in "+e)
					noteStat[e]=max(noteStat[e],elements[e][id1])
					output+=(", now "+e+" = "+str(noteStat[e]))
					noteStat["total"]+=1
					output+="<p/>"
	return(output)
	
def checkModel(word):
	output=""
	for id in plp_rec(word):
		if ("A" == plp_label(id)[0]):
			if 1 in plp_vec(id,word):
				output+=("MIANOWNIK: " + word+ " - " + plp_bform(id) +", as " + str(plp_vec(id,word)) + " - "+plp_label(id))
				output+="<p/>"
			if 3 in plp_vec(id,word):
				output+=("CELOWNIK: " + word+ " - " + plp_bform(id) +", as " + str(plp_vec(id,word)) + " - "+plp_label(id))
				output+="<p/>"
			if 4 in plp_vec(id,word):
				output+=("BIERNIK: " + word+ " - " + plp_bform(id) +", as " + str(plp_vec(id,word)) + " - "+plp_label(id))
				output+="<p/>"
	return(output)
	

def computeScore():
	return (noteStat["Places"]+1)*(noteStat["Events"]+1)+(noteStat["Objects"])+(noteStat["Extras"])+(noteStat["total"])
					
					
					
def go(note,lvl):		
 
	output=""
	for fileName in filesList:
		output+=readFile(fileName)

	scoreInit()	
		
	for word in note.split():
		word=word.strip(".\":?!\W+")
		output+=str(checkWord(word))
		 
		 
	output+="Compute score for note as :   "+"("+str(noteStat["Places"])+"+1) * ("+str(noteStat["Events"])+"+1) + "+\ 
	str(noteStat["Objects"])+" + "+str(noteStat["Extras"])+" + "+str(noteStat["total"])
		
	if 	(computeScore() >int(lvl)):  
		flag="<br/><br/><big>:) That text is what you are looking for!</big><p/>"
	else: 
		flag="<br/><br/><big>:( That text is not what you are looking for...</big><p/>"	
	return (lvl+ " "+flag+"<br/>Total score for note = "+str(computeScore()))+"<p/>"+ output

def gomodel(note):		
 
	output=""

		
	for word in note.split():
		output+=str(checkModel(word.strip(".\":?!\W+")))
		if '.' in word :
			output+="#############<p/>"
		
	return (output)

 
#run(note) 
	        
	
	